<?php

namespace JLPay\Services\Ledger\BalanceSplit;

use JLPay\Services\BaseService;
use JLPay\Models\Ledger\BalanceSplit\BalanceQueryRequest;
use JLPay\Models\Ledger\BalanceSplit\BalanceQueryResponse;
use JLPay\Models\Ledger\BalanceSplit\BalanceSplitRequest;
use JLPay\Models\Ledger\BalanceSplit\BalanceSplitResponse;
use JLPay\Models\Ledger\BalanceSplit\BalanceSplitQueryRequest;
use JLPay\Models\Ledger\BalanceSplit\BalanceSplitQueryResponse;
use JLPay\Models\Ledger\BalanceSplit\BalanceRefundRequest;
use JLPay\Models\Ledger\BalanceSplit\BalanceRefundResponse;
use JLPay\Models\Ledger\BalanceSplit\BalanceRefundQueryRequest;
use JLPay\Models\Ledger\BalanceSplit\BalanceRefundQueryResponse;

/**
 * 余额分账服务类
 */
class BalanceSplitService extends BaseService
{
    /**
     * 获取API URL
     *
     * @param string $requestClass 请求类名
     * @param string|null $apiPath 接口路径
     * @return string 完整的API URL
     */
    public function getApiUrl(string $requestClass, ?string $apiPath = null): string
    {
        $baseUrl = $this->config->getUrl();

        if ($apiPath) {
            return $baseUrl . $apiPath;
        }

        // 直接使用请求类中定义的完整API路径
        if (defined($requestClass . '::API_URL')) {
            $apiUrl = constant($requestClass . '::API_URL');
            return $baseUrl . $apiUrl;
        }

        // 如果没有定义API_URL，使用默认路径
        return $baseUrl . '/open/ledger/balancesplit';
    }

    /**
     * 余额查询
     *
     * @param BalanceQueryRequest $request 请求对象
     * @return BalanceQueryResponse 响应对象
     */
    public function queryBalance(BalanceQueryRequest $request): BalanceQueryResponse
    {
        return $this->post($request, BalanceQueryResponse::class);
    }

    /**
     * 余额分账
     *
     * @param BalanceSplitRequest $request 请求对象
     * @return BalanceSplitResponse 响应对象
     */
    public function splitBalance(BalanceSplitRequest $request): BalanceSplitResponse
    {
        return $this->post($request, BalanceSplitResponse::class);
    }

    /**
     * 余额分账查询
     *
     * @param BalanceSplitQueryRequest $request 请求对象
     * @return BalanceSplitQueryResponse 响应对象
     */
    public function queryBalanceSplit(BalanceSplitQueryRequest $request): BalanceSplitQueryResponse
    {
        return $this->post($request, BalanceSplitQueryResponse::class);
    }

    /**
     * 余额退款
     *
     * @param BalanceRefundRequest $request 请求对象
     * @return BalanceRefundResponse 响应对象
     */
    public function refundBalance(BalanceRefundRequest $request): BalanceRefundResponse
    {
        return $this->post($request, BalanceRefundResponse::class);
    }

    /**
     * 余额退款查询
     *
     * @param BalanceRefundQueryRequest $request 请求对象
     * @return BalanceRefundQueryResponse 响应对象
     */
    public function queryBalanceRefund(BalanceRefundQueryRequest $request): BalanceRefundQueryResponse
    {
        return $this->post($request, BalanceRefundQueryResponse::class);
    }
} 